﻿using System;
using System.Drawing;

namespace GMapNET
{
   internal class Tile
   {
      TileCoordinate tile;
      IntPtr ptrHbitmap;
      Image image;
      RenderMode mode;

      public Tile(Image image, TileCoordinate tile, RenderMode mode)
      {
         this.tile = tile;
         if(image != null)
         {
            this.mode = mode;

            switch(mode)
            {
               case RenderMode.GDI:
               {
                  using(Bitmap bitmap = new Bitmap(image))
                  {
                     this.ptrHbitmap = bitmap.GetHbitmap();
                  }
               }
               break;

               case RenderMode.GDI_PLUS:
               {
                  this.image = image;
               }
               break;
            }
         }
      }

      public void Clear()
      {
         switch(mode)
         {
            case RenderMode.GDI:
            {
               if(ptrHbitmap != IntPtr.Zero)
               {
                  NativeMethods.DeleteObject(ptrHbitmap);
                  ptrHbitmap = IntPtr.Zero;
               }
            }
            break;

            case RenderMode.GDI_PLUS:
            {
               if(image!= null)
               {
                  image.Dispose();
                  image = null;
               }
            }
            break;
         }

         if(tile != null)
         {
            tile = null;
         }
      }

      public TileCoordinate Coordinate
      {
         get
         {
            return tile;
         }
      }

      public IntPtr Hbitmap
      {
         get
         {
            return ptrHbitmap;
         }
      }

      public Image Image
      {
         get
         {
            return image;
         }
      }
   }
}
